package Service;


import Dao.CustomerDao;
import Dto.Customer;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomerService {
    private CustomerDao customerDao = new CustomerDao();

    public Map<String, Object> selectCustomerByPage(Integer pageNum, String username) throws SQLException {
        List<Customer> list = customerDao.selectCustomerList(pageNum, username);
        Long totalCount = Long.valueOf(list.size());
        Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("totalCount", totalCount);
        map.put("totalPageNum", totalPageNum);
        return map;
    }

    public Map<String, Object> selectSuperCustomerByPage(Integer pageNum) throws SQLException {
        List<Customer> list = customerDao.selectSuperCustomerList(pageNum);
        Long totalCount = customerDao.selectCustomerCount();
        Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("totalCount", totalCount);
        map.put("totalPageNum", totalPageNum);
        return map;
    }

    public void addCustomer(Customer customer) throws SQLException {
        customerDao.addCustomer(customer);
    }

    public Customer preUpdateCustomer(Integer id) throws SQLException {
        return customerDao.preUpdateCustomer(id);
    }

    public void updateCustomer(Customer customer) throws SQLException {
        customerDao.updateCustomer(customer);
    }

    public void deleteCustomer(Integer id) throws SQLException {
        customerDao.deleteCustomer(id);
    }

    public Map<String, Object> selectAll(String username, String name, String company) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        if (name != null && !name.equals("")) {
            List<Customer> nameList = customerDao.selectByName(username, name);
            Long totalCount = Long.valueOf(nameList.size());
            Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
            map.put("list", nameList);
            map.put("totalCount", totalCount);
            map.put("totalPageNum", totalPageNum);
            return map;
        }
        if (company != null && !company.equals("")) {
            List<Customer> companyList = customerDao.selectByCompany(username, company);
            Long totalCount = Long.valueOf(companyList.size());
            Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
            map.put("list", companyList);
            map.put("totalCount", totalCount);
            map.put("totalPageNum", totalPageNum);
            return map;
        }
        List<Customer> list = customerDao.selectAllByUser(username);
        Long totalCount = Long.valueOf(list.size());
        Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
        map.put("totalCount", totalCount);
        map.put("totalPageNum", totalPageNum);
        map.put("list", list);
        return map;
    }

    public Map<String, Object> selectSuperAll(String name, String company) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        if (name != null && !name.equals("")) {
            List<Customer> nameList = customerDao.selectSuperByName(name);
            Long totalCount = Long.valueOf(nameList.size());
            Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
            map.put("list", nameList);
            map.put("totalCount", totalCount);
            map.put("totalPageNum", totalPageNum);
            return map;
        }
        if (company != null && !company.equals("")) {
            List<Customer> companyList = customerDao.selectSuperByCompany(company);
            Long totalCount = Long.valueOf(companyList.size());
            Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
            map.put("list", companyList);
            map.put("totalCount", totalCount);
            map.put("totalPageNum", totalPageNum);
            return map;
        }
        List<Customer> list = customerDao.selectSuperAll();
        Long totalCount = customerDao.selectCustomerCount();
        Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
        map.put("totalCount", totalCount);
        map.put("totalPageNum", totalPageNum);
        map.put("list", list);
        return map;
    }
}